using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ScytheEngine
{

    public class Quad : ScytheEngine.Object,
        IRenderable,
        ILoadable
    {
        #region Data Members

        private string m_Asset;

        private Texture2D m_Texture;
        private IndexBuffer m_Indices;
        private VertexBuffer m_Vertices;

        #endregion

        #region Ctors

        public Quad(string asset)
        {
            m_Asset = asset;
        }

        #endregion

        #region IRenderable Members

        public void Render(GraphicsDevice gfxDev)
        {
            using (VertexDeclaration declaration = new VertexDeclaration(gfxDev, VertexPositionTexture.VertexElements))
            {
                gfxDev.VertexDeclaration = declaration;

                gfxDev.Textures[0] = m_Texture;
                gfxDev.Vertices[0].SetSource(m_Vertices, 0, VertexPositionTexture.SizeInBytes);

                gfxDev.Indices = m_Indices;
                gfxDev.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
            }
        }

        #endregion

        #region ILoadable Members

        public void LoadContent(GraphicsDevice gfxDev, Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            m_Texture = loader.Load<Texture2D>(m_Asset) as Texture2D;

            VertexPositionTexture[] vertices = 
            {
                new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.0f), new Vector2(0,0)),
                new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.0f), new Vector2(1,0)),
                new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.0f), new Vector2(0,1)),
                new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.0f), new Vector2(1,1))
            };

            int[] indices = { 0, 1, 2, 1, 3, 2 };

            m_Vertices = new VertexBuffer(gfxDev, typeof(VertexPositionTexture), 4, BufferUsage.WriteOnly);
            m_Vertices.SetData<VertexPositionTexture>(vertices, 0, 4);

            m_Indices = new IndexBuffer(gfxDev, typeof(int), 6, BufferUsage.WriteOnly);
            m_Indices.SetData<int>(indices, 0, 6);
        }

        #endregion
    }
}
